<?php
namespace app\task\lib;

use app\task\models\TaskModel;
use lib\redis\RedisTool;
use lib\task\TaskJob;

class TaskHandle{

    protected static $obj;
    protected static $objName;
    public static function  addTask(TaskJob $obj,$objName){
        self::$obj[]=$obj;
        self::$objName[] = $objName;
        return true;
    }

    public static function toTask(){
        $TaskModel = new TaskModel();
        foreach (self::$obj as $k=>$v){
            $time = time();
            $values = ['task_delay'=>$v->delay(),'task_frequency'=>$v->frequency(),'task_add_time'=>$time,'task_name'=>self::$objName[$k],'task_param'=>$v->param(),'task_src'=>$v->objName(),'task_md5'=>md5(self::$objName[$k].$v->objName())];
            $values = $TaskModel->setTaskLog($values);
            if (empty($values['task_id'])){
                RedisTool::_init(0)->setKey('TASK_LIST')->setListLeft(json_encode($values));
            }
        }
        return true;
    }

}